/*
 * (C) Copyright 2015-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     ohun@live.cn (夜色)
 */

package com.shinemo.mpush.alloc;

import java.util.concurrent.Executors;

import com.mpush.api.push.PushSender;
import com.mpush.api.service.BaseService;
import com.mpush.api.service.Listener;
import com.mpush.netty.http.HttpClient;
import com.mpush.netty.http.NettyHttpClient;
import com.mpush.tools.config.CC;
import com.mpush.tools.log.Logs;
import com.shinemo.mpush.alloc.handler.*;
import com.shinemo.mpush.alloc.service.MesIdService;
import com.shinemo.mpush.alloc.service.chat.IosPushService;
import com.sun.net.httpserver.HttpServer;

/**
 * Created by yxx on 2016/5/6.
 *
 * @author ohun@live.cn
 */
public final class AllocServer extends BaseService {

	private HttpServer httpServer;
	private AllocHandler allocHandler;
	private final PushSender pushSender = PushSender.create();
	private final HttpClient httpClient = new NettyHttpClient();
	private final MesIdService mesIdService = new MesIdService();
	private IosPushService iosPushService=null;

	@Override
	public void init() {

		iosPushService=new IosPushService(httpClient);

		int port = CC.mp.net.cfg.getInt("alloc-server-port");
		boolean https = "https".equals(CC.mp.net.cfg.getString("alloc-server-protocol"));

		this.httpServer = HttpServerCreator.createServer(port, https);
		this.allocHandler = new AllocHandler();
		PushHandler pushHandler = new PushHandler(pushSender,httpClient,mesIdService,iosPushService);
		ChatMessagesHandler chatMessagesHandler = new ChatMessagesHandler(pushSender,httpClient,mesIdService);
		SessionHandler sessionHandler = new SessionHandler(pushSender,mesIdService);
		MemberHandler memberHandler = new MemberHandler(pushSender,httpClient,mesIdService);
		MessageNotificationHandler MessageNotificationHandler = new MessageNotificationHandler(pushSender,mesIdService);

		httpServer.setExecutor(Executors.newCachedThreadPool());// 设置线程池，由于是纯内存操作，不需要队列
		httpServer.createContext("/", allocHandler);// 查询mpush机器
		httpServer.createContext("/push", pushHandler);// 模拟发送push
		httpServer.createContext("/chatMessages", chatMessagesHandler);

		httpServer.createContext("/session",sessionHandler);
		httpServer.createContext("/member",memberHandler);
		httpServer.createContext("/messageNotification",MessageNotificationHandler);

//		httpServer.createContext("/index.html", new IndexPageHandler());//
//		httpServer.createContext("/session.html", new PageHandler("/session.html"));
//		httpServer.createContext("/chat.html", new PageHandler("/chat.html"));

	}

	@Override
	protected void doStart(Listener listener) throws Throwable {
		allocHandler.start();
		httpServer.start();
		pushSender.start();
		httpClient.syncStart();
		Logs.Console.info("===================================================================");
		Logs.Console.info("====================ALLOC SERVER START SUCCESS=====================");
		Logs.Console.info("===================================================================");
	}

	@Override
	protected void doStop(Listener listener) throws Throwable {
		httpServer.stop(0);// 1 min
		allocHandler.stop();
		pushSender.stop();
		httpClient.syncStop();
		Logs.Console.info("===================================================================");
		Logs.Console.info("====================ALLOC SERVER STOPPED SUCCESS=====================");
		Logs.Console.info("===================================================================");
	}
}
